
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        picPath: app.globalData.picPath,
        tipText: app.globalData.tipText,// 顶部提示语
        type: '1',//收入类型
        typeText: '工资、薪金所得(综合所得)',//收入类型文字
        money: '',// 税前工资
        money1: '',// 专项扣除（社保公积金个人部分）
        money2: '',// 专项附加扣除（子女教育等）
        money3: '',
        jianMoney: '5000',// 基本减除费用（起征点）
        showTypePop: false,//选择性别和人员类型弹窗
        actions: [
            {
                name: '工资、薪金所得(综合所得)',
                value: '1',
            },
            {
                name: '年终奖所得',
                value: '2',
            },
            {
                name: '劳动报酬所得',
                value: '3',
            },
            {
                name: '稿酬所得',
                value: '4',
            },
            // {
            //     name: '特许权使用费所得',
            //     value: '5',
            // },
            // {
            //     name: '经营所得',
            //     value: '6',
            // },
            {
                name: '利息、股息、红利所得',
                value: '7',
            },
            // {
            //     name: '财产租赁所得',
            //     value: '8',
            // },
            // {
            //     name: '财产转让所得',
            //     value: '9',
            // },
            // {
            //     name: '偶然所得',
            //     value: '10',
            // },
        ],
        navList: [],//底部导航栏
        showPop: false,
        resList: [{
            text: '税前收入',
            money: 10
        },
        {
            text: '应纳税所得额',
            money: 0
        },
        {
            text: '适用税率',
            money: 0
        },
        {
            text: '速算扣除数',
            money: 0
        },
        {
            text: '应纳税额',
            money: 0
        },
        {
            text: '税后收入',
            money: 0
        },

        ],

        title1: '税前工资',
        title2: '专项扣除（社保公积金个人部分）',
        checked: false,//  是否为 个人出租房屋
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var navList = app.globalData.navList, nowPage = '/' + this.is;
        navList = navList.filter(item => {
            return item.path != nowPage
        })
        this.setData({
            navList
        })
        console.log(this.data);

    },
    onChange(e) {
        console.log(e);
        this.setData({ checked: e.detail });
    },
    toNav(e) {
        console.log(e);
        var path = e.currentTarget.dataset.nav.path;
        console.log(path);
        if (path == '') {
            wx.showToast({
                title: '抱歉，功能暂未开通',
                icon: 'none'
            })
            return
        }
        wx.redirectTo({
            url: path,
        })
    },
    // 关闭选择类型弹窗
    cancelHandle() {
        this.setData({
            showTypePop: false
        })
    },

    closeHandle() {
        this.setData({
            showPop: false
        })
    },

    // 确认类型
    selectHandle(e) {
        console.log(e.detail);
        var title1 = '', title2 = '';
        if (e.detail.value == 1) {//工资、薪金所得(综合所得)
            title1 = '税前工资'
            title2 = '专项扣除（社保公积金个人部分）*'
        } else if (e.detail.value == 2) { // 年终奖所得
            title1 = '年终奖所得'
            title2 = '当月工资（不含社保）'
        } else if (e.detail.value != 1 && e.detail.value != 2) { //劳动报酬所得  稿酬所得 特许权使用费所得 经营所得    利息、股息、红利所得   财产租赁所得   财产转让所得  偶然所得
            title1 = '收入金额'
        }

        this.setData({
            typeText: e.detail.name,
            type: e.detail.value,
            showTypePop: false,
            title1,
            title2,
            money1: '',
            money2: ''
        })
    },

    // 显示选择类型弹窗
    chooseType() {
        this.setData({
            showTypePop: true
        })
    },
    // 关闭选择类型弹窗
    cancelHandle() {
        this.setData({
            showTypePop: false
        })
    },


    checkHandle() {
        if (this.data.type == '') {
            wx.showToast({
                title: '请选择收入类型',
                icon: 'none'
            })
            return
        }

        // 工资、薪金所得(综合所得)
        if (this.data.type == 1) {
            if (this.data.money == '') {
                wx.showToast({
                    title: '请输入税前工资',
                    icon: 'none'
                })
                return
            }

            if (this.data.money1 == '') {
                wx.showToast({
                    title: '请输入专项扣除（社保公积金个人部分）',
                    icon: 'none'
                })
                return
            }
            if (this.data.money2 == '') {
                wx.showToast({
                    title: '请输入专项附加扣除（子女教育等）',
                    icon: 'none'
                })
                return
            }
            console.log(this.data.money - this.data.money1 - this.data.money2);
            if ((this.data.money - this.data.money1 - this.data.money2) <= 5000) {
                wx.showModal({
                    title: '提示',
                    content: '您无需缴纳个人所得税',
                    showCancel: false,
                    complete: (res) => {
                        if (res.confirm) {

                        }
                    }
                })
                return
            }
        } else if (this.data.type == 2) {//年终奖所得
            if (this.data.money == '') {
                wx.showToast({
                    title: '请输入' + this.data.title1,
                    icon: 'none'
                })
                return
            }
            if (this.data.money1 == '') {
                wx.showToast({
                    title: '请输入' + this.data.title2,
                    icon: 'none'
                })
                return
            }
        } else if (this.data.type != 1 && this.data.type != 2) {  // 劳动报酬所得  稿酬所得  特许权使用费所得  经营所得   利息、股息、红利所得  财产租赁所得  财产转让所得   偶然所得
            if (this.data.money == '') {
                wx.showToast({
                    title: '请输入' + this.data.title1,
                    icon: 'none'
                })
                return
            }
        }
        console.log(this.data);






        var that = this, resList = [], url = '',
            data = {};

        if (that.data.type == 1) {  // 工资、薪金所得(综合所得)
            url = app.globalData.url + "tool/oneCountTax"
            data = {
                wages: that.data.money,
                special: that.data.money1,
                additional: that.data.money2,
            }
        } else if (that.data.type == 2) {  //年终奖所得
            url = app.globalData.url + "tool/yearCountTax"
            data = {
                special: that.data.money,
                wages: that.data.money1,
            }
        } else if (that.data.type == 3) {  //劳动报酬
            url = app.globalData.url + "tool/lCountTax"
            data = {
                wages: that.data.money,
            }
        } else if (that.data.type == 4) {   // 稿酬所得
            url = app.globalData.url + "tool/gCountTax"
            data = {
                wages: that.data.money,
            }
        } else if (that.data.type == 7) {   // 利息、股息、红利所得
            url = app.globalData.url + "tool/countInterest"
            data = {
                amount: that.data.money,
            }
        }

        wx.showLoading({
            title: '计算中',
            icon: ''
        })

        app.wxRequest(url, data, res => {
            if (res.flag) {
                wx.hideLoading()
                console.log(res.data);
                // 工资、薪金所得(综合所得)
                if (that.data.type == 1) {
                    res.data.ynssde = res.data.ynssde.toFixed(2)
                    res.data.ss = res.data.ss.toFixed(2)
                    res.data.ynse = res.data.ynse.toFixed(2)
                    res.data.shsr = res.data.shsr - that.data.money1
                    res.data.shsr = res.data.shsr.toFixed(2)
                    resList = [
                        {
                            text: '税前收入',
                            money: '￥' + Number(that.data.money).toLocaleString()
                        },
                        {
                            text: '应纳税所得额',
                            money: '￥' + Number(res.data.ynssde).toLocaleString()
                        },
                        {
                            text: '适用税率',
                            money: res.data.shuilv + '%'
                        },
                        {
                            text: '速算扣除数',
                            money: '￥' + Number(res.data.ss).toLocaleString()
                        },
                        {
                            text: '应纳税额',
                            money: '￥' + Number(res.data.ynse).toLocaleString()
                        },
                        {
                            text: '税后收入',
                            money: '￥' + Number(res.data.shsr).toLocaleString()
                        },
                    ]
                } else if (that.data.type == 2) {  //年终奖所得
                    res.data.pingjun = res.data.pingjun.toFixed(2)
                    res.data.ss = res.data.ss.toFixed(2)
                    res.data.ynse = res.data.ynse.toFixed(2)
                    res.data.shsr = res.data.shsr.toFixed(2)
                    resList = [
                        {
                            text: '年终奖金',
                            money: '￥' + Number(that.data.money).toLocaleString()
                        },
                        {
                            text: '平均每月工资',
                            money: '￥' + Number(res.data.pingjun).toLocaleString()
                        },
                        {
                            text: '适用税率',
                            money: res.data.shuilv + '%'
                        },
                        {
                            text: '速算扣除数',
                            money: '￥' + Number(res.data.ss).toLocaleString()
                        },
                        {
                            text: '应纳税额',
                            money: '￥' + Number(res.data.ynse).toLocaleString()
                        },
                        {
                            text: '税后收入',
                            money: '￥' + Number(res.data.shsr).toLocaleString()
                        },

                    ]
                } else if (that.data.type == 3) { //劳动报酬所得
                    res.data.jcfy = res.data.jcfy.toFixed(2)
                    res.data.yingnashuisuodee = res.data.yingnashuisuodee.toFixed(2)
                    res.data.ss = res.data.ss.toFixed(2)
                    res.data.yingnashuie = res.data.yingnashuie.toFixed(2)
                    res.data.shuihoushouru = res.data.shuihoushouru.toFixed(2)
                    resList = [
                        {
                            text: '税前收入',
                            money: '￥' + Number(that.data.money).toLocaleString()
                        },
                        {
                            text: '减除费用',
                            money: '￥' + Number(res.data.jcfy).toLocaleString()
                        },
                        {
                            text: '应纳税所得额',
                            money: '￥' + Number(res.data.yingnashuisuodee <= 0 ? 0 : res.data.yingnashuisuodee).toLocaleString()
                        },
                        {
                            text: '适用税率',
                            money: res.data.yingnashuisuodee > 0 ? res.data.shiyongshuilv + '%' : 0 + '%'
                        },
                        {
                            text: '速算扣除数',
                            money: '￥' + Number(res.data.ss).toLocaleString()
                        },
                        {
                            text: '应纳税额',
                            money: '￥' + Number(res.data.yingnashuie <= 0 ? 0 : res.data.yingnashuie).toLocaleString()
                        },
                        {
                            text: '税后收入',
                            money: '￥' + Number(res.data.yingnashuie > 0 ? res.data.shuihoushouru : that.data.money).toLocaleString()
                        },
                    ]

                } else if (that.data.type == 4) {  //稿酬所得
                    res.data.jcfy = res.data.jcfy.toFixed(2)
                    res.data.yingnashuisuodee = res.data.yingnashuisuodee.toFixed(2)
                    res.data.yingnashuie = res.data.yingnashuie.toFixed(2)
                    res.data.shuihoushouru = res.data.shuihoushouru.toFixed(2)
                    resList = [
                        {
                            text: '税前收入',
                            money: '￥' + Number(that.data.money).toLocaleString()
                        },
                        {
                            text: '减除费用',
                            money: '￥' + Number(res.data.jcfy).toLocaleString()
                        },
                        {
                            text: '应纳税所得额',
                            money: '￥' + Number(res.data.yingnashuisuodee <= 0 ? 0 : res.data.yingnashuisuodee).toLocaleString()
                        },
                        {
                            text: '适用税率',
                            money: res.data.yingnashuisuodee > 0 ? res.data.shiyongshuilv + '%' : 0 + '%'
                        },
                        {
                            text: '应纳税额',
                            money: '￥' + Number(res.data.yingnashuie <= 0 ? 0 : res.data.yingnashuie).toLocaleString()
                        },
                        {
                            text: '税后收入',
                            money: '￥' + Number(res.data.yingnashuie > 0 ? res.data.shuihoushouru : that.data.money).toLocaleString()
                        },
                    ]


                } else if (that.data.type == 5) {//特许权使用费所得
                    resList = [
                        {
                            text: '税前收入',
                            money: that.data.money
                        },]
                } else if (that.data.type == 6) {//经营所得
                    resList = [
                        {
                            text: '税前收入',
                            money: that.data.money
                        },]
                } else if (that.data.type == 7) {//利息、股息、红利所得
                    res.data.interest = res.data.interest.toFixed(2)
                    res.data.taxableIncome = res.data.taxableIncome.toFixed(2)
                    resList = [
                        {
                            text: '税前收入',
                            money: '￥' + Number(that.data.money).toLocaleString()
                        },
                        {
                            text: '应纳税所得额',
                            money: '￥' + Number(that.data.money).toLocaleString()
                        },
                        {
                            text: '适用税率',
                            money: '20%'
                        },
                        {
                            text: '应纳税额',
                            money: '￥' + Number(res.data.interest).toLocaleString()

                        },
                        {
                            text: '税后收入',
                            money: '￥' + Number(res.data.taxableIncome).toLocaleString()
                        },
                    ]
                } else if (that.data.type == 8) {  //财产租赁所得
                    resList = [
                        {
                            text: '税前收入',
                            money: that.data.money
                        },
                        {
                            text: '减除费用',
                            money: ''
                        },
                        {
                            text: '应纳税所得额',
                            money: ''
                        },
                        {
                            text: '适用税率',
                            money: ''
                        },
                        {
                            text: '应纳税额',
                            money: ''
                        },
                        {
                            text: '税后收入',
                            money: ''
                        },
                    ]
                } else if (that.data.type == 9) {  // 财产转让所得
                    resList = [
                        {
                            text: '税前收入',
                            money: that.data.money
                        },
                        {
                            text: '应纳税所得额',
                            money: ''
                        },
                        {
                            text: '适用税率',
                            money: ''
                        },
                        {
                            text: '应纳税额',
                            money: ''
                        },
                        {
                            text: '税后收入',
                            money: ''
                        },
                    ]
                } else if (that.data.type == 10) {  // 偶然所得
                    resList = [
                        {
                            text: '税前收入',
                            money: that.data.money
                        },
                        {
                            text: '应纳税所得额',
                            money: ''
                        },
                        {
                            text: '适用税率',
                            money: ''
                        },
                        {
                            text: '应纳税额',
                            money: ''
                        },
                        {
                            text: '税后收入',
                            money: ''
                        },
                    ]
                }

                that.setData({
                    resList, showPop: true
                })


                // wx.showToast({
                //   title: '修改成功',
                //   icon: 'none'
                // })
                // wx.hideLoading()
            }
        }, err => { })
    },
    resHandle() {
        wx.showModal({
            title: '提示',
            content: '是否重置信息',
            complete: (res) => {
                if (res.cancel) {
                }
                if (res.confirm) {
                    this.setData({
                        money: '',
                        money1: '',
                        money2: '',
                        jianMoney: '5000',
                    })
                }
            }
        })
    },
    inputHandle(e) {
        var str = e.currentTarget.dataset.str
        this.setData({
            [str]: e.detail.value
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})